<?php
class Student
{
	private $id;
	private $name;
	private $birth;
	private $gender;

    /**
     * 选课集合
     * @var Course
     */
    private $courseSelection = [];
    

    public function __construct($id,$name,$age,$gender)
    {
    	$this->setId($id);
    	$this->setName($name);
    	$this->setAge($age);
    	$this->setGender($gender);
    }

    // 魔术方法，属性读取器 $name为属性名
    public function __get($name)
    {
    	$method = 'get' . ucfirst(strtolower($name));
    	// 判断方法名method 是否为空
    	if (method_exists($this, $method)) {
    		return $this->$method();
    	}
    	echo '方法' . $method . '不存在';
    	return false;
    }

    // 魔术方法，属性读取器 $name为属性名 $value为属性值
    public function __set($name,$value)
    {
    	$method = 'set' . ucfirst(strtolower($name));
    	// 判断方法名为$method的方法是否存在
    	if (method_exists($this,$method)) {
    		$this->$mathod($value);
    	}
    }
    
    // 添加新课程
    public function addCourse(Courses $course)
    {
    		$this->courseSelection[] = $course;
    }

    /**
     * 删除选课
     * @param $courseId 要删除的课程Id
     */
    public function removeCourse($courseId)
    {
    	$index = -1;
    	for ($i = 0;$i < count($this->courseSelection);$i++) {
    		if ($this->courseSelection[$i]->id == $courseId) {
    			$index = $i;
    			break;
    		}
    	}
    	if ($index > -1) {
    		array_splice($this->courseSelection, $course,1);
    	}
    }

    public function study()
	{
		echo $this->name . '将要学习一下课程' . '<br/>';
		foreach ($this->courseSelection as $course) {
		 	echo $course;
		 } 
		 echo '<br/>';
	}

	private function getId()
	{
		return $this->id;
	}

	private function setId($id)
	{
		if (is_integer($id)&& $id > 0){
			$this->id = $id;
		}else {
		    echo 'id不合法';
	    } 
	}

	private function getName()
	{
		return $this->name;
	}

	private function setName($name)
	{
		if (is_string($name)) {
			$this->name = $name;
		}
	}

	private function getAge()
	{
		return '生于' . date('Y',$this->birth);
	}

	private function setAge($age)
	{
		if (is_integer($age)) {
		$this->birth = time() - $age * 365 * 24 * 3600 ;
	    }
	}

	private function getGender()
	{
		return $this->gender == 1 ? '男' : '女';
	}

	private function setGender($gender)
	{
		if (is_integer($gender)) {
			$this->gender = $gender;
		}
	}
}